#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>


int main(){
    pid_t id=fork();
    if(id==0){
        //子进程
        printf("before: I am a childprocess, pid: %d, ppid: %d\n",getpid(),getppid());
        sleep(1);
        //标准写法
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        //execlp("ls", "ls", "-a", "-l", NULL);
        
        //char  *argv[]={"ls", "-a", "-l", NULL};
        //execv("/usr/bin/ls", argv);
        //execvp("ls",argv);
        
        //extern char **environ;
        //putenv("ABC=123");
        //execle("./test","test",NULL,environ);
        
        char const *new_env[]={"ABC=123","DEF=456",NULL};
        //execle("./test","test",NULL, new_env);
        char const *argv[]={"test",NULL};
        execvpe("./test",argv,new_env);
        printf("after: I am a process, pid: %d, ppid: %d\n",getpid(),getppid());
        exit(0);
    }
    else if(id>0){
        //父进程
        pid_t ret=waitpid(id,NULL,0);
        printf("wait success, pid: %d, ret: %d\n",getpid(),ret);
        sleep(1);
    }
    return 0;
}
